package com.game.gameObject
{
	import com.game.gameHandle.GraphicsResource;
	import com.game.gameHandle.ZOrders;
	
	import flash.display.*;
	import flash.geom.*;
	
	import mx.collections.*;
	import mx.core.*;
	
	public class TiledBackground extends BaseObject
	{
		public var tiles:Array = new Array();

		public var tileWidth:int = 0;
		public var tileHeight:int = 0;
		
		//static public var pool:ResourcePool = new ResourcePool(NewTiledBackground);
		
//		static public function NewTiledBackground():TiledBackground
//		{
//			 return new TiledBackground();
//		}
		
		public function TiledBackground()
		{
			super();
		}
		
		public function startupTiledBackground():void
		{
			super.startupBaseObject(ZOrders.BACKGROUNDZORDER);
		}
		
		override public function shutdown():void
		{
			super.shutdown();
		}
		
		override public function copyToBackBuffer(db:BitmapData):void
		{
		 	
		 	var drawnHeight:int = 0;
		 	var drawnWidth:int = 0;	
		 	
		 	var layer:int = 0;
		 	var row:int = 0;
		 	var col:int = 0;
		 	
			// 循环每层
			for (layer = 0; layer < this.tiles.length; ++layer)
			{
				// 每行
				var count:int = 0;
				for (row = 0; row < this.tiles[layer].length; ++row)
				{
			 		// 每列
			 		for (col = 0; col < this.tiles[layer][row].length; ++col)
			 		{
			 			var graphics:GraphicsResource = this.tiles[layer][row][col] as GraphicsResource;
			 			
			 			if (graphics != null)
			 			{			 							 			
				 			db.copyPixels(
				 				graphics.bitmap,
				 				graphics.drawRect, 
				 				new Point(
				 					col * this.tileWidth, 
				 					row * this.tileHeight),
				 				graphics.bitmapAlpha, 
				 				new Point(
				 					graphics.drawRect.x, 
				 					graphics.drawRect.y), 
				 				true);
			 			}
			 				
			 			drawnWidth += this.tileWidth;
			 			if (drawnWidth >= Application.application.myCanvas.width)
			 				break;
			 		}
			 		
			 		drawnWidth = 0;
			 		drawnHeight += this.tileHeight;
			 		
			 		if (drawnHeight >= Application.application.myCanvas.height + this.tileHeight)
			 			break;			 		
			 	}
			 	
			 	drawnHeight = 0;
		 	}
		}
	}
}